#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>
#include <unistd.h>

#include "model/model.h"
#include "model/print_utils.h"
#include "index/index.h"
#include "saturation/saturation.h"
#include "hierarchy/hierarchy.h"

extern int yyparse(void);
FILE *yyin;

struct timeval tStart, tEnd;
#define START_TIMER	gettimeofday(&tStart, NULL)
#define STOP_TIMER	gettimeofday(&tEnd, NULL)
#define TIME_DIFF	(((double)tEnd.tv_sec * 1000000.0 + \
		(double)tEnd.tv_usec) - \
		((double)tStart.tv_sec * 1000000.0 + (double)tStart.tv_usec) )


int main(int argc, char *argv[]) {

	// parser return code
	int parser;

	double total_time = 0.0;

	if (argc <= 1) {
		fprintf(stderr,"syntax: %s input_kb\n", argv[0]);
		return -1;
	}

	yyin = fopen(argv[1], "r");
	assert(yyin != NULL);

	// initialize global variables, allocate space
	init_reasoner();

	printf("Loading KB.........................");
	fflush(stdout);
	START_TIMER;
	parser = yyparse();
	STOP_TIMER;
	total_time += TIME_DIFF;
	if (parser != 0) {	
		print_short_stats();
		fprintf(stderr,"aborting\n");
		exit(-1);
	}
	printf("%.3f milisecs\n", TIME_DIFF / 1000);

	printf("Indexing...........................");
	fflush(stdout);
	START_TIMER;
	index_tbox(tbox);
	STOP_TIMER;
	printf("%.3f milisecs\n", TIME_DIFF / 1000);
	total_time += TIME_DIFF;

	printf("Saturating..........................");
	fflush(stdout);
	START_TIMER;
	init_saturation();
	saturate_tbox(tbox);
	STOP_TIMER;
	printf("%.3f milisecs\n", TIME_DIFF / 1000);
	total_time += TIME_DIFF;

	printf("Computing concept hierarchy........");
	fflush(stdout);
	START_TIMER;
	compute_concept_hierarchy(tbox);
	STOP_TIMER;
	printf("%.3f milisecs\n", TIME_DIFF / 1000);
	total_time += TIME_DIFF;

	printf("Total time.........................%.3f milisecs\n", total_time / 1000);

	print_short_stats();
	// print_tbox(tbox);

	getchar();

	return 0;
}

